﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IForm_EditString</summary>
    public class IForm_EditString : IForm
    {
        public string Value; // Hodnota pre editaciu
        public string Caption; // Podnadpis okna
        public int MaxLength; // Max. dlzka vysledku
        public bool CanBeEmpty; // Priznak moznosti prazdneho vysledku
        public string AutoCompleteData; // Data pre autocomplete mod
        public string AutoCompleteResourceName; // Nazov resource pre autocomplete mod
        public bool PasswordMode; // Priznak modu zadavania hesla

        protected IGroupBox FGrp; // Controls
        protected IButton FBtn_OK;
        protected IButton FBtn_Cancel;
        protected IAutoHeightLabel FLabel;
        protected ITextBox FTextBox;

        /// <summary>Konstruktor</summary>
        public IForm_EditString()
        {
            Value = "";
        }

        /// <summary>Vytvorenie obsahu formulara</summary>
        /// <returns>true / false</returns>
        protected override bool OnCreateForm()
        {
            if (!base.OnCreateForm()) return false; // Volam predchodcu

            StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; // Nastavenie vlastnosti
            MinimizeBox = MaximizeBox = false; ShowIcon = true; ShowInTaskbar = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;

            Width = 430; Height = 300; System.Drawing.Size CS = ClientSize; // Ziskam vnutorne rozmery


            FGrp = new IGroupBox(); IControl.PlaceControl_Inside(FGrp, this, 10, 10, 10, 50); // Group box
            FBtn_Cancel = new IButton(lang.FORM_EDIT_STRING_TEXT_2); IControl.PlaceControl_Outside_TopRight_RightBottom(FBtn_Cancel, FGrp, 0, 10); FBtn_Cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            FBtn_OK = new IButton(lang.FORM_EDIT_STRING_TEXT_1, Event_BtnOKClick); IControl.PlaceControl_Outside_TopRight_LeftTop(FBtn_OK, FBtn_Cancel, 0, 10);

            FLabel = new IAutoHeightLabel(Caption, FGrp.ClientSize.Width - 16);
            FTextBox = new ITextBox(); FTextBox.Text = Value.Trim(); if (PasswordMode) FTextBox.PasswordChar = '*';
            if (MaxLength != -1) FTextBox.MaxLength = MaxLength;

            FGrp.Controls.AddRange(new System.Windows.Forms.Control[] { FLabel, FTextBox }); // Zaradim do controls
            IControl.PlaceControl_Inside_LeftTop_LeftTop(FLabel, FGrp, 8, 16);
            IControl.PlaceControl_Outside_LeftTop_LeftBottom(FTextBox, FLabel, 0, 8, FGrp.ClientSize.Width - 16);

            LoadAutoComplete();

            Controls.AddRange(new System.Windows.Forms.Control[] { FGrp, FBtn_OK, FBtn_Cancel }); // Zaradim do controls

            CancelButton = FBtn_Cancel; AcceptButton = FBtn_OK; // Nastavenie dialogovych tlacidiel

            int H = (FGrp.ClientSize.Height - FTextBox.Bottom - 12); // Upravim velkost formulara
            FGrp.Height -= H; FBtn_Cancel.Top -= H; FBtn_OK.Top -= H; Height -= H;

            return true; // Vsetko OK
        }

        /// <summary>Nacitanie autocomplete</summary>
        protected void LoadAutoComplete()
        {
            if (!string.IsNullOrEmpty(AutoCompleteResourceName))
            {
                FTextBox.EnableAutocomplete(ICore.Base64Decode(IApp.GlobalData.App_HistoryLists.GetParam(AutoCompleteResourceName, "")));
            }
            else
            {
                if (!string.IsNullOrEmpty(AutoCompleteData)) FTextBox.EnableAutocomplete(AutoCompleteData);
            }
        }
        /// <summary>Ulozenie autocomplete</summary>
        protected void SaveAutoComplete()
        {
            if (!string.IsNullOrEmpty(AutoCompleteResourceName))
            {
                string Data = FTextBox.UpdateAutoComplete();
                IApp.GlobalData.App_HistoryLists.SetParam(AutoCompleteResourceName, ICore.Base64Encode(Data));
            }
        }

        /// <summary>Event: OnClick pre tlacidlo FBtn_OK</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnOKClick(object pSender, EventArgs e)
        {
            string Val = FTextBox.Text.Trim(); // Ziskam hodnotu
            if (string.IsNullOrEmpty(Val) && (!CanBeEmpty))
            {
                IApp.ShowError(lang.FORM_EDIT_STRING_TEXT_3);
                FTextBox.Focus(); // Nastavim fokus
                return;
            }
            Value = Val; // Ulozim vysledok
            SaveAutoComplete();
            DialogResult = System.Windows.Forms.DialogResult.OK; // Ukoncim dialog
        }

    }
}
